import {createRouter, createWebHistory} from "vue-router";
import LocalStoreUtil from "@/utils/LocalStoreUtil.js";

const routes = [
    {
        name: 'redirect',
        path: '/',
        component: () => import("@/pages/index/index.vue"),
    }, {
        name: 'index',
        path: '/index',
        component: () => import('@/pages/index/index.vue'),
        meta: {
            title: '饼干标签页'
        }
    }, {
        name: 'login',
        path: '/login',
        component: () => import('@/pages/login/index.vue'),
        meta: {
            title: '饼干标签页'
        },
        children: [
            {
                name: 'login-default',
                path: '/login',
                component: () => import('@/components/login/PasswordLoginForm.vue'),
                meta: {
                    title: '饼干标签页-密码登录'
                }
            }, {
                name: 'PasswordLogin',
                path: '/login/password',
                component: () => import('@/components/login/PasswordLoginForm.vue'),
                meta: {
                    title: '饼干标签页-密码登录'
                }
            }, {
                name: 'code',
                path: '/login/code',
                component: () => import('@/components/login/CodeLoginForm.vue'),
                meta: {
                    title: '饼干标签页-验证码登录'
                }
            }, {
                name: 'RegisterLogin',
                path: '/login/register',
                component: () => import('@/components/login/RegisterLoginForm.vue'),
                meta: {
                    title: '饼干标签页-注册登录'
                }
            }, {
                name: 'ResetPassword',
                path: '/login/reset',
                component: () => import('@/components/login/ResetPasswordForm.vue'),
                meta: {
                    title: '饼干标签页-重置密码'
                }
            }
        ]
    }, {
        name: "system-setting",
        path: "/setting",
        component: () => import('@/pages/setting/index.vue'),
        meta: {
            title: '设置中心'
        },
        children: [
            {
                name: 'default',
                path: '/setting',
                component: () => import('@/components/setting/Basic.vue'),
                meta: {
                    title: '设置中心'
                }
            }, {
                name: 'setting-user',
                path: '/setting/user',
                component: () => import('@/components/setting/Basic.vue'),
                meta: {
                    title: '设置中心'
                }
            }, {
                name: 'setting-background',
                path: '/setting/background',
                component: () => import('@/components/setting/Basic.vue'),
                meta: {
                    title: '设置中心'
                }
            }, {
                name: 'setting-help',
                path: '/setting/help',
                component: () => import('@/components/setting/Basic.vue'),
                meta: {
                    title: '设置中心'
                }
            }, {
                name: 'setting-recommend',
                path: '/setting/recommend',
                component: () => import('@/components/setting/Basic.vue'),
                meta: {
                    title: '设置中心'
                }
            }, {
                name: 'setting-guide',
                path: '/setting/guide',
                component: () => import('@/components/setting/Basic.vue'),
                meta: {
                    title: '设置中心'
                }
            },
        ]
    }, {
        name: "file",
        path: "/imageUpload",
        component: () => import('@/components/common/ImageUpload.vue'),
        meta: {
            title: '设置中心'
        }
    }, {
        name: "chat",
        path: "/chat/:chatRoomId",
        component: () => import('@/pages/chat/index.vue'),
        meta: {
            title: '聊天'
        }
    }, {
        name: 'toolsNavView',
        path: '/tools',
        component: () => import('@/view/tools_index_View.vue'),
        meta: {
            title: '工具导航中心',
        },
        children: [
            {
                name: 'toolsDefaultView',
                path: '/tools',
                component: () => import('@/view/tools/tools_nav_View.vue')
            }, {
                name: 'toolsNavView',
                path: '/tools/nav',
                component: () => import('@/view/tools/tools_nav_View.vue')
            }, {
                name: 'excelToolView',
                path: '/tools/excel',
                component: () => import('@/view/tools/excel_tool_View.vue')
            }
        ]
    },

    // 用户信息中心页面路由

    {
        name: 'userInfo',
        path: '/user_info',
        component: () => import('@/pages/user/index.vue'),
        meta: {
            requiresAuth: true, // 需要登录授权访问
        }
    },

    {
        name: 'ai',
        path: '/chat_ai',
        component: () => import('@/pages/ai/index.vue')
    },

    // 用户信息中心页面路由

    // view 模块相关组件路由 ======================================== start
    {
        name: 'viewIndex',
        path: '/view',
        component: () => import('@/view/view_index.vue'),
        children: [
            {
                name: 'filePartUpload',
                path: '/view/file_part_upload',
                component: () => import('@/view/interview/file_part_upload_View.vue')
            }
        ]
    },
    // view 模块相关组件路由 ======================================== end


    {
        name: '404',
        path: '/:pathMatch(.*)*',
        component: () => import('@/pages/error/index.vue')
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    // 判断登录页面是否需要登录
    if (to.meta.requiresAuth && !LocalStoreUtil.isLogin()) {
        next({
            path: 'login',
            query: {
                redirectUrl: to.path
            }
        })
        return;
    }

    // 判断即将要离开的页面，url 中是否包含 redirectUrl 参数，包含则跳转到指定 redirectUrl 地址指向的页面
    if (from.query.redirectUrl) {
        const redirectUrl = from.query.redirectUrl;
        delete from.query.redirectUrl;
        document.title = from.meta.title || '饼干标签页';
        next(redirectUrl);
    } else {
        document.title = to.meta.title || '饼干标签页';
        next();
    }
});

export default router;